package com.wayhome.srpingbootmybatis.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.SimpleFormatter;

public class DateUtilTes {
    public static Date addDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    public static void main(String[] args) throws ParseException {
//        lastDayByCurrentYear();
        LocalDate start = LocalDate.of(2024, 8, 1);
        LocalDate end = LocalDate.of(2025, 9,10);
        isWithinOneYear(start, end);
    }


    private void comparer() throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date0301 = simpleDateFormat.parse("2025-03-01 00:00:00");
//        Date date = addDay(parseDate, -1);
//
//        String format = simpleDateFormat.format(date);
//        System.out.println(format);
        Date date0310 = simpleDateFormat.parse("2025-03-10 00:00:00");

        LocalDate localDate0301 = LocalDate.of(2025,3,1);
        LocalDate localDate0310 = LocalDate.of(2025,3,11);

        long between = ChronoUnit.DAYS.between(localDate0301, localDate0310);
        System.out.println("相隔时间：" + between + "天" +
                "");


        Date date = addDay(new Date(), -1);
        System.out.println(simpleDateFormat.format(date));

    }

    private static void lastDayByCurrentYear() {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().getYear(), 12, 31, 23, 59,59);
        Date date = Date.from(localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
    }

    public static boolean isWithinOneYear(LocalDate date1, LocalDate date2) {
        long yearsBetween = Math.abs(ChronoUnit.YEARS.between(date1, date2));
        System.out.println("相差年数：" + yearsBetween);
        return yearsBetween <= 1;
    }
}
